/**
 * Created by bai on 2015/7/27.
 */
var Member = function(){
    var htmlMemberList = function(option){

        $('   <li data-function="btn_memberList_oneInfo" data-id="'+option.id+'">\
                    <span class="memberImg" data-function="btn_memberList_oneInfo" data-id="'+option.id+'"></span>\
            <p data-function="btn_memberList_oneInfo" data-id="'+option.id+'"> '+option.memberName+'，电话：'+option.memberMobile+'</p>\
        </li>').appendTo('#subMemberSearch .memberList');
        $('<li class="vipInfo" data-function="btn_orderingFood_chooseMember" data-id="'+option.id+'">\
            <div class="vipIco" data-function="btn_orderingFood_chooseMember"></div>\
            <p class="info" data-function="btn_orderingFood_chooseMember"> '+option.memberName+'，电话：'+option.memberMobile+'</p>\
        </li>').appendTo('#subVipMainPage .vipList');
    }
    /*
     * 会员入口
     * */
    var memberEntrance = function(fun){
        var memberListParents = $('#subMemberSearch')
        OrderingSystem.getMemberList(function (data) {
            $('.memberList',memberListParents).html("");
            fun();
            $.each(data.data,function(k,v){
                htmlMemberList({
                    id:v.id,
                    memberName:v.memberName,
                    memberMobile:v.memberMobile
                })
                /*   $('   <li data-function="btn_memberList_oneInfo" data-id="'+v.id+'">\
                 <span class="memberImg" data-function="btn_memberList_oneInfo" data-id="'+v.id+'"></span>\
                 <p data-function="btn_memberList_oneInfo" data-id="'+v.id+'"> '+v.memberName+'，电话：'+v.memberMobile+'。。。</p>\
                 </li>').appendTo('#subMemberSearch .memberList');*/
            })
        });

    };
    /*
     * 搜索会员
     * */
    var seekMemmber = function(){
        var parentNode = $('#subMemberSearch')
        var text = "";
        if($(".searchTxt",parentNode).val()){
            text = $(".searchTxt",parentNode).val()
        }else{
            text = $('#subVipMainPage .searchText').val()
        }
        OrderingSystem.searchMember({
            "keyword"        : text,
            "successFunction": function (data) {
                $('.memberList',parentNode).html("");
                $('#subVipMainPage .vipList').html("");
                $.each(data.data,function(k,v){
                    htmlMemberList({
                        id:v.id,
                        memberName:v.memberName,
                        memberMobile:v.memberMobile
                    })
                })
            }
        });
    }
    /*
     * 支付流水子函数
     * */
    var payWater = function(mId){
        OrderingSystem.getMemberAccountTrans(mId, function (data) {
            var parentsNode = $('#subMemberDetailed');
            $('.memberWater',parentsNode).html('');
            $(' <dd>\
                    <div class="leftTitle">日期</div>\
                    <div class="rightTitle">业务</div>\
                    <div class="clear"></div>\
                    </dd>').appendTo('#subMemberDetailed .memberWater');
            $.each(data.data,function(k,v){
                var time = new Date(v.time.sec*1000);
                time = time.getFullYear()+"."+(time.getMonth()+1)+"."+time.getDate();
                if(v.transType=="recharge"){
                    $('<dd>\
                       <p class="leftPart" data-function="btn_member_recharge_info" data-id="'+v.id+'">'+time+'</p>\
                   <p class="rightPart" data-function="btn_member_recharge_info" data-id="'+v.id+'">充'+v.transAmount/100+'元赠'+v.point/100+'消费点</p>\
                   <div class="rightPartbak" data-function="btn_member_recharge_info" data-id="'+v.id+'"></div>\
                       </dd>').appendTo('#subMemberDetailed .memberWater')
                }else{
                    $('<dd>\
                           <p class="leftPart" data-function="btn_member_consumed_info" data-time="'+time+'" data-memberid="'+v.transMemberId+'" data-consume="'+(-v.point/100)+'" data-id="'+v.id+'" data-orderid="'+v.transOrderId+'">'+time+'</p>\
                       <p class="rightPart" data-function="btn_member_consumed_info" data-time="'+time+'" data-memberid="'+v.transMemberId+'" data-consume="'+(-v.point/100)+'" data-id="'+v.id+'" data-orderid="'+v.transOrderId+'">消费'+(-v.point/100)+'元</p>\
                       <div class="rightPartbak" data-function="btn_member_consumed_info" data-time="'+time+'" data-memberid="'+v.transMemberId+'" data-consume="'+(-v.point/100)+'" data-id="'+v.id+'" data-orderid="'+v.transOrderId+'"></div>\
                           </dd>').appendTo('#subMemberDetailed .memberWater')
                }



            })
        });
    }
    /*
     * 每条会员的详情
     * */
    var oneMemberInfo = function(obj,fun){
        var parentsNode = $('#subMemberDetailed');
        var mId = obj.data('id');
        OrderingSystem.getMemberById(mId, function (data) {
            $('.memberOneInfo',parentsNode).html("");
            $('.btn',parentsNode).html("");
            fun();
            var dataPort = data.data[0];
            $('.activeOrNoActive',parentsNode).remove();
            if(dataPort.status=="active"){
                $('.indexMask',parentsNode).hide();
                $('<div class="SlideLeft activeOrNoActive" data-function="btn_member_setMemberStatus" data-id="'+dataPort.id+'">\
                <div class="SlideBtn" data-function="btn_member_setMemberStatus" data-id="'+dataPort.id+'"></div>\
                    <span> 激活</span>\
                </div>').prependTo('#subMemberDetailed')
                /*  $('.activeOrNoActive',parentsNode).removeClass('SlideRight');
                 $('.activeOrNoActive',parentsNode).addClass('SlideLeft');*/
            }else{
                $('<div class="SlideRight activeOrNoActive" data-function="btn_member_setMemberStatus" data-id="'+dataPort.id+'">\
                <div class="SlideBtn" data-function="btn_member_setMemberStatus" data-id="'+dataPort.id+'"></div>\
                    <span> 冻结</span>\
                </div>').prependTo('#subMemberDetailed')
                $('.indexMask',parentsNode).show();
                /*    $('.activeOrNoActive',parentsNode).removeClass('SlideLeft');
                 $('.activeOrNoActive',parentsNode).addClass('SlideRight');*/
            }

            $('       <div class="TopButton disabled" data-function="btn_member_recharge_ok" data-id="'+dataPort.id+'">确定</div>\
               <div class="PrintButton" data-function="btn_member_printerWater" data-id="'+dataPort.id+'">打印流水</div>').appendTo('#subMemberDetailed .btn')
            $('  <li>\
                <div class="DetailedTitle">姓名</div>\
                <p class="green">'+dataPort.memberName+'</p>\
                </li>\
                <li>\
                    <div class="DetailedTitle">电话</div>\
                    <p class="green">'+dataPort.memberMobile+'</p>\
                </li>\
                <li>\
                <div class="DetailedTitle">充值</div>\
                <p><input class="conSpan">元</p>\
                </li>\
                <li>\
                    <div class="DetailedTitle">赠</div>\
                    <p><input class="conSpan costume" >消费点</p>\
                    </li>\
                    <li>\
                        <div class="DetailedTitle">-累积信息</div>\
                    </li>').appendTo('#subMemberDetailed .memberOneInfo');

            payWater(mId);//会员的流水
        });

    }
    /*
     * 添加会员
     * */
    var addMember = function(fun){
        var parentNode = $('#subAddMember');
        $('.AddBtn',parentNode).addClass('disabled');
        $(".spanBorderA",parentNode).val("");
        $(".spanBorderB",parentNode).val("");
        $(".spanBorderC",parentNode).val("");
        $(".pointSpend",parentNode).val("");
        fun();

    }
    /*
     * 添加会员Ok
     * */
    var addMemberOk = function(fun){
        var parentNode = $('#subAddMember');
        var member = {
            "memberName"  : $.trim($(".spanBorderA",parentNode).val()),//会员名字
            "memberMobile": $.trim($(".spanBorderB",parentNode).val()),//会员的电话
            "level"       : '1',//会员等级 A B C
            "memberMemo"  : ""//备注
        };
        OrderingSystem.addMember(member, function (data) {
            if(data.error>0){
                OrderingSystem.getMemberList(function (data) {
                    var nameReapte = false;
                    $.each(data.data,function(k,v){
                        if(v.memberMobile==member.memberMobile){
                            nameReapte=true;
                        }

                    })
                    if(nameReapte){
                        alert("会员电话重复，请重新输入");
                        nameReapte=true;
                    }
                });
            }else{
                var mId = data.data[0].id;
                var amount = $(".spanBorderC",parentNode).val()*100;
                var point = $(".pointSpend",parentNode).val()*100;
                var paymentMethod = 'cash';
                var memo = '';
                OrderingSystem.memberCharge(mId, amount, point, paymentMethod, memo, function (data) {
                    memberEntrance(fun);
                });
            }




        });

    }
    /*
     *会员的充值详情
     * */
    var memberRechargeInfo = function(obj,fun){
        var options = {};
        options.transId = obj.data('id');
        options.successFunction = function (data) {
            var dataPort = data.data[0];
            var time = new Date(dataPort.time.sec*1000);

            time = time.getFullYear()+"."+(time.getMonth()+1)+"."+time.getDate();
            OrderingSystem.getMemberById(dataPort.transMemberId,function(dataMId){
                $('#subMemberBill').html("");
                fun();
                $('<li>\
                <p class="left">日期：'+time+'</p>\
            </li><li>\
                <p class="left">充值'+dataPort.transAmount/100+'元&nbsp;&nbsp;&nbsp;&nbsp;</p>\
                <p class="left">赠'+dataPort.point/100+'消费点</p>\
            </li><div class="Balance">\
                <p class="left">余额：'+dataMId.data[0].residuePoint/100+'元</p>\
                <p class="right">消费点：'+dataMId.data[0].residuePoint/100+'点</p>\
            </div>').appendTo('#subMemberBill');
            })



        };
        OrderingSystem.getMemberTransByTransId(options);

    }
    /*
     * 会员的消费详情
     * */
    var memberConsumedInfo = function(obj,fun){
        var oId = obj.data('orderid');
        OrderingSystem.getOrderById(oId, function (data) {
            var dataOfPort=data.data[0];
            var cpList="";

            var request='';
            var token="";
            var tokenId=[];
            var isNotPay = '';
            if(dataOfPort.tokenList[0]){
                token=dataOfPort.tokenList[0].name;
            }else{
                token='无'
            }
            $.each(dataOfPort.itemList,function(k,v){
                cpList+='<li>\
                    <p class="left">'+v.name+'</p>\
                    <p class="right">*'+v.count+' ￥'+v.price/100+'</p>\
                    </li>'
            })
            OrderingSystem.getMemberById(obj.data('memberid'),function(dataByMemberId){
                $('#subMemberBill').html('');
                $('<li>\
                <p class="left">日期：'+obj.data('time')+'</p>\
            <p class="right">消费'+obj.data('consume')+'元</p>\
            </li>\
           '+cpList+'\
                <li>\
                    <p class="left">合计：'+dataOfPort.amount/100+'元</p>\
                    <p class="right">优惠：'+token+'</p>\
                </li>\
                <div class="Balance">\
                    <p class="left">余额：'+dataByMemberId.data[0].residuePoint/100+'消费点</p>\
                </div>').appendTo('#subMemberBill')
            })





        });
        fun();
    }
    /*
     * 会员充值ok
     * */
    var memberRechargeOk = function(obj){
        var parensNode = $('#subMemberDetailed');
        var mId = obj.data('id');
        var amount = $(".conSpan",parensNode).val()*100;
        var point = $(".costume",parensNode).val()*100;
        var paymentMethod = 'cash';
        var memo = '';
        OrderingSystem.memberCharge(mId, amount, point, paymentMethod, memo, function (data) {

            payWater(mId);
            $(".conSpan",parensNode).val("");
            $(".costume",parensNode).val("");
        });
    }
    /*
     * 打印会员流水
     * */
    var printerMemberWater = function(obj){
        OrderingSystem.printerMemberAccountTrans({
            "memberId"       : obj.data('id'),
            "successFunction": function (data) {
            }
        });
    }
    /*
     * 给会员的充值的输入框，加上keyup事件
     * */
    var memberInputRecharge = function(){
        var parentsNode = $('#subMemberDetailed');
        $(parentsNode).on('keyup','.conSpan',function(){
            if($.trim($('.conSpan',parentsNode).val())){
                $('.TopButton',parentsNode).addClass('active');
                $('.TopButton',parentsNode).removeClass('disabled');
            }else{
                $('.TopButton',parentsNode).removeClass('active');
                $('.TopButton',parentsNode).addClass('disabled');
            }
        })

    }();
    /*
     * 添加会员的输入框加上事件
     * */
    var addInputKeyup = function(){
        var parentsNode = $('#subAddMember');
        /*    $(parentsNode).on('keyup','.pointSpend',function(){
         if($.trim($('.pointSpend',parentsNode).val())&&$.trim($('.spanBorderA',parentsNode).val())&&$.trim($('.spanBorderB',parentsNode).val())&&$.trim($('.spanBorderC',parentsNode).val())){
         $('.AddBtn',parentsNode).addClass('active');
         $('.AddBtn',parentsNode).removeClass('disabled');
         }else{
         $('.AddBtn',parentsNode).removeClass('active');
         $('.AddBtn',parentsNode).addClass('disabled');
         }
         })*/
        $(parentsNode).on('keyup',function(ev){
            if($.trim($('.pointSpend',parentsNode).val())&&$.trim($('.spanBorderA',parentsNode).val())&&$.trim($('.spanBorderB',parentsNode).val())&&$.trim($('.spanBorderC',parentsNode).val())){
                $('.AddBtn',parentsNode).addClass('active');
                $('.AddBtn',parentsNode).removeClass('disabled');
            }else{
                $('.AddBtn',parentsNode).removeClass('active');
                $('.AddBtn',parentsNode).addClass('disabled');
            }
        })
    }();
    /*
     * 冻结会员或者激活会员
     * */
    var freezeOrActiveMember = function(obj){
        var parentNode = $('#subMemberDetailed');
        if($('.activeOrNoActive',parentNode).hasClass('SlideLeft')){
            var mId = obj.data('id');
            var memo = "deactive";
            OrderingSystem.setMemberStatus(mId, memo, function (data) {
                $('.indexMask',parentNode).show();
                $('.activeOrNoActive',parentNode).addClass('SlideRight');
                $('.activeOrNoActive',parentNode).removeClass('SlideLeft');
                $('.activeOrNoActive span',parentNode).html("冻结");
            });
        }else{
            var mId = obj.data('id');
            var memo = "active";
            OrderingSystem.setMemberStatus(mId, memo, function (data) {
                $('.indexMask',parentNode).hide();
                $('.activeOrNoActive',parentNode).removeClass('SlideRight');
                $('.activeOrNoActive',parentNode).addClass('SlideLeft');
                $('.activeOrNoActive span',parentNode).html("激活");
            });

        }


    }
    return{
        /*会员入口*/memberEntrance:function(fun){
            memberEntrance(fun);
        },
        /*搜索会员*/seekMemmber:function(){
            seekMemmber()
        },
        /*一条会员的详情*/oneMemberInfo:function(obj,fun){
            oneMemberInfo(obj,fun);
        },
        /*添加会员*/addMember:function(fun){
            addMember(fun)
        },
        /*添加会员OK*/addMemberOk:function(fun){
            addMemberOk(fun);
        },
        /*会员充值详情*/memberRechargeInfo:function(obj,fun){
            memberRechargeInfo(obj,fun);
        },
        /*会员消费详情*/memberConsumedInfo:function(obj,fun){
            memberConsumedInfo(obj,fun);
        },
        /*会员充值Ok*/memberRechargeOk:function(obj){
            memberRechargeOk(obj);
        },
        /*打印会员流水*/printerMemberWater:function(obj){
            printerMemberWater(obj);
        },
        /*冻结会员或者激活会员*/freezeOrActiveMember:function(obj){
            freezeOrActiveMember(obj)
        }
    }
}();